import java.io.*;
import java.util.*;

/*
 * Class name: CourseIO
 * Description: Used to make an array list from a txt file full of course names
 */
public class CourseIO {

	public static ArrayList<Course> getCourses(){
		ArrayList<Course> courses = new ArrayList<Course>();
		BufferedReader in = getReader("courses.txt");
		Course course = readCourse(in);
		while (course != null){
			courses.add(course);
			course = readCourse(in);
		}
		return courses;
	}
	
	private static BufferedReader getReader(String name){
		BufferedReader in = null;
		try {
			File file = new File(name);
			in = new BufferReader(new FileReader(file));
		}
		catch (FileNotFoundException e){
			System.out.println("The file in question does not exist, please check the path and try again.");
			System.exit(0);
		}
		catch (IOException e){
			System.out.println("I/O Exception.")
			System.exit(0);
		}
		return in;
	}
	
	private static Course readCourse(BufferedReader in){
		String name;
		String coursename;
		int timeHour;
		int timeMinute;
		String line = "";
		String data[];
		
		try{
			line = in.readLine();
		}
		catch (IOException e){
			System.out.println("I/O Exception.")
			System.exit(0);
		}
		if (line == null){
			return null;
		}
		else{
			data = line.split("\t");
			name = data[0];
			coursename = data[1];
			time = Integer.parseInt(data[2]);
			return new Course(name, coursename, timeHour, timeMinute);
		}
	}
}